﻿Imports System
Imports System.Net

Namespace Framework
    Public NotInheritable Class NetIPAddress
        Public Shared Function GetIPAddress()
            '' This function retrieves the IP Address from the registry
            '' It gets it from the CurrentControlSet, so even when using DHCP 
            '' it returns the correct IP Address

            '' Declare variables

            'Dim key
            'Dim cTempIPAddress
            'Dim cIPAddress
            'Dim cIPAddressKey


            'oSh = CreateObject("WScript.Shell")

            'cInterfacesKey = "HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\"
            'cNICSearch = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\1\ServiceName"


            '' First check which network card to use
            'cNicServiceName = oSh.RegRead(cNICSearch)

            '' Now read the IP Address from that card
            'cIPAddressKey = cInterfaceskey + cNicServiceName + "\IPAddress"
            'cTempIPAddress = oSh.RegRead(cIPAddressKey)

            '' Split the items in the var tempIPAddress to the octets in array IPAddress
            'cIPAddress = Split(cTempIPAddress(0), ".", 4)

            '' the IP addresss is now readable from ipaddress
            '' for example, to read the first octet, use: FirstOctet=IPAddress(0)

            'GetIPAddress = cIPAddress
        End Function

        Public Shared Function GetIPOctet(ByVal nOctet)
            ' This function retrieves a given octet out of the IP Address
            Dim IPAddress

            IPAddress = GetIPAddress()
            GetIPOctet = IPAddress(nOctet - 1)
        End Function
    End Class

    Public Class GetMacAddressFromIPAddress
        Public Function GetMacAddress(ByVal IPAddress As String) As String
            Dim strMacAddress As String = String.Empty
            Try
                Dim strTempMacAddress As String = String.Empty
                Dim objProcessStartInfo As ProcessStartInfo = New ProcessStartInfo
                Dim objProcess As Process = New Process
                objProcessStartInfo.FileName = "nbtstat"
                objProcessStartInfo.RedirectStandardInput = False
                objProcessStartInfo.RedirectStandardOutput = True
                objProcessStartInfo.Arguments = "-A " + IPAddress
                objProcessStartInfo.UseShellExecute = False
                objProcess = Process.Start(objProcessStartInfo)

                Dim Counter As Integer = -1
                While (Counter <= -1)
                    Counter = strTempMacAddress.Trim().ToLower().IndexOf("mac address", 0)
                    If (Counter > -1) Then
                        Exit While
                    End If
                    strTempMacAddress = objProcess.StandardOutput.ReadLine()
                End While
                objProcess.WaitForExit()
                strMacAddress = strTempMacAddress.Trim()
            Catch ex As Exception
                Console.WriteLine(ex.ToString())
                Console.ReadLine()
            End Try
            Return strMacAddress
        End Function

        Public Sub ss()
            Dim IPAddress As String = "xxx.xxx.xxx.xxx"
            Dim objGetMacAddress As GetMacAddressFromIPAddress = New GetMacAddressFromIPAddress
            Dim MacAddress As String = objGetMacAddress.GetMacAddress(IPAddress)
            Console.WriteLine(MacAddress)
            Console.ReadLine()
        End Sub
    End Class
End Namespace